package com.miyu.system.service.book.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.Comutils;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.system.dao.book.BookMainDao;
import com.miyu.system.pojo.book.BookMain;
import com.miyu.system.utils.system.SystemUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 书本保存任务作业
 * @author miyu
 * @date 2022/11/25
 */
public class BookSaveTaskJob implements Callable<Boolean> {

    BookMainDao dao = (BookMainDao) GetBeanUtil.getBeanByClass(BookMainDao.class);
    private JSONObject params;
    private String type;
    @Override
    public Boolean call() throws Exception {
        if("0".equals(type)) {
            bookMainSave();
        }else{
            bookReaderSave();
        }
        return null;
    }

    private void bookMainSave() {
        JSONObject bookInfo = params.getJSONObject("main");
        BookMain bookMain = JSONObject.parseObject(bookInfo.toString(),BookMain.class);
        JSONArray chater = params.getJSONArray("chapter");
        //随机保存到表数据
        String roundTable = SystemUtils.getTable();
        String ruleId =  bookMain.getRuleId();
        //书主表
        String bookMainTable = "book_main_"+ruleId;
        //章节表
        String bookChapterTable = "book_chapter_"+ruleId+"_"+roundTable;
        //对应表 不再加前缀
        String source = ruleId+"_"+roundTable;
        System.out.println("章节对应表"+bookChapterTable);
        List<String> sqlList = new ArrayList<String>();
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into ") .append(bookMainTable).append(" (fpk,rule_id,book_source,book_name,book_chapter_source,book_author,book_kind,book_state,book_cover");
       sql.append("   ,book_duce");
        sql.append(" ) ");
        sql.append(" values (");
        sql.append("'").append(bookMain.getFpk()).append("',");
        sql.append("'").append(ruleId).append("',");
        sql.append("'").append(bookInfo.getString("bookUrl")).append("',");
        sql.append("'").append(bookMain.getBookName()).append("',");
        sql.append("'").append(source).append("',");
        if (bookInfo.containsKey("bookAuthor")) {
            sql.append("'").append(bookInfo.getString("bookAuthor")).append("',");
        } else {
            sql.append(" null,");
        }
        if (bookInfo.containsKey("bookKind")) {
            sql.append("'").append(bookInfo.getString("bookKind")).append("',");
        } else {
            sql.append(" null,");
        }

        if (bookInfo.containsKey("bookState")) {
            sql.append("'").append(bookInfo.getString("bookState")).append("',");
        } else {
            sql.append(" null,");
        }
        if (bookInfo.containsKey("bookCover")) {
            sql.append("'").append(bookInfo.getString("bookCover")).append("',");
        } else {
            sql.append(" null,");
        }
        if (bookInfo.containsKey("bookDuce")) {
            sql.append("'").append(bookInfo.getString("bookDuce")).append("')");
        } else {
            sql.append(" null)");
        }
        sqlList.add(sql.toString());

        for (int i = 0; i < chater.size(); i++) {
            JSONObject chaterItem = chater.getJSONObject(i);
            sql.setLength(0);
            sql.append(" insert into  ").append(bookChapterTable).append(" (");
            sql.append(" fpk,book_id,chapter_source,chapter_name,chapter_num)");
            sql.append(" values (");
            sql.append(" '").append(Comutils.MD5(chaterItem.getString("chapterUrl"))).append("',");
            sql.append(" '").append(bookMain.getFpk()).append("',");
            sql.append(" '").append(chaterItem.getString("chapterUrl")).append("',");
            sql.append(" '").append(chaterItem.getString("chapterName")).append("',");
            sql.append(" ").append(chaterItem.getString("num")).append(")");
            sqlList.add(sql.toString());
        }
        //保存小说明细
         dao.doExecuteBatch(sqlList);

    }
    private void bookReaderSave() {
        StringBuffer sql = new StringBuffer();
        String tableId = "book_reader_"+params.getString("chapterTable");
        sql.append("insert into " +tableId).append("(fpk,chapter_text) values(");
        sql.append("'").append(params.getString("fpk")).append("',");
        sql.append("'").append(params.getString("chapterContext")).append("')");
        dao.doExecute(sql.toString());
    }


    public BookSaveTaskJob(JSONObject params,String type)  {
        this.params = params;
        this.type=type;
    }
}
